|
1
|
|
|
/* jshint jquery: true, strict: false */ |
|
2
|
|
|
|
|
3
|
|
|
/* global klass: false, jQuery: false */ |
|
4
|
|
|
|
|
5
|
|
|
var VatNumberCheckObject = { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A css class selector to trigger `check` logic. |
|
9
|
|
|
* |
|
10
|
|
|
* @type {string} |
|
11
|
|
|
*/ |
|
12
|
|
|
elementSelector: '', |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* An url to use for checking. |
|
16
|
|
|
* |
|
17
|
|
|
* @type {string} |
|
18
|
|
|
*/ |
|
19
|
|
|
checkUrl: '', |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* An object with (status) images to use as background after checking. |
|
23
|
|
|
* |
|
24
|
|
|
* @type {Object} |
|
25
|
|
|
*/ |
|
26
|
|
|
checkImages: {}, |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* Initializes instance variables and attaches a blur event. |
|
32
|
|
|
* |
|
33
|
|
|
* Possible keys: |
|
34
|
|
|
* - ok |
|
35
|
|
|
* - failure |
|
36
|
|
|
* - serviceUnavailable |
|
37
|
|
|
* |
|
38
|
|
|
* @param {Object} options An options object |
|
39
|
|
|
*/ |
|
40
|
|
|
initialize: function (options) { |
|
41
|
|
|
this.elementSelector = options.elementSelector || this.elementSelector; |
|
42
|
|
|
this.checkUrl = options.checkUrl || this.checkUrl; |
|
43
|
|
|
this.checkImages = options.checkImages || this.checkImages; |
|
44
|
|
|
|
|
45
|
|
|
jQuery(this.elementSelector).blur(jQuery.proxy(this._check, this)); |
|
46
|
|
|
}, |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* A blur event handler. |
|
50
|
|
|
* |
|
51
|
|
|
* Requests the check url and displays it's status. |
|
52
|
|
|
* |
|
53
|
|
|
* @access protected |
|
54
|
|
|
* @param {Object} event |
|
55
|
|
|
*/ |
|
56
|
|
|
_check: function (event) { |
|
57
|
|
|
var element = jQuery(event.target); |
|
58
|
|
|
|
|
59
|
|
|
jQuery.ajax({ |
|
60
|
|
|
type: 'POST', |
|
61
|
|
|
url: this.checkUrl, |
|
62
|
|
|
beforeSend: jQuery.proxy(this._beforeSend, this, element), |
|
63
|
|
|
data: { |
|
64
|
|
|
vatNumber: element.val() |
|
65
|
|
|
} |
|
66
|
|
|
}).done( |
|
67
|
|
|
jQuery.proxy(this._done, this, element) |
|
68
|
|
|
).fail( |
|
69
|
|
|
jQuery.proxy(this._fail, this, element) |
|
70
|
|
|
); |
|
71
|
|
|
}, |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* A request handler for `done`. |
|
75
|
|
|
* |
|
76
|
|
|
* @access protected |
|
77
|
|
|
* @param {Object} element A DOM element |
|
78
|
|
|
* @param {Object} result Request data |
|
79
|
|
|
*/ |
|
80
|
|
|
_done: function (element, result) { |
|
81
|
|
|
if (result.status === 'ok') { |
|
82
|
|
|
this._setBackground(element, this.checkImages.ok); |
|
83
|
|
|
} else { |
|
84
|
|
|
if (result.vatNumber.length === 0) { |
|
85
|
|
|
this._setBackground(element); |
|
86
|
|
|
} else { |
|
87
|
|
|
this._setBackground(element, this.checkImages.failure); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
element.val(result.vatNumber); |
|
92
|
|
|
}, |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* A request handler for `fail`. |
|
96
|
|
|
* |
|
97
|
|
|
* @access protected |
|
98
|
|
|
* @param {Object} element A DOM element |
|
99
|
|
|
*/ |
|
100
|
|
|
_fail: function (element) { |
|
101
|
|
|
this._setBackground(element, this.checkImages.serviceUnavailable); |
|
102
|
|
|
}, |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* A request handler for `beforeSend`. |
|
106
|
|
|
* |
|
107
|
|
|
* @access protected |
|
108
|
|
|
* @param {Object} element A DOM element |
|
109
|
|
|
*/ |
|
110
|
|
|
_beforeSend: function (element) { |
|
111
|
|
|
this._setBackground(element, ''); |
|
112
|
|
|
}, |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Changes the background image of a given element. |
|
116
|
|
|
* |
|
117
|
|
|
* @access protected |
|
118
|
|
|
* @param {Object} element A DOM element |
|
119
|
|
|
* @param {string} image An image (url) |
|
120
|
|
|
*/ |
|
121
|
|
|
_setBackground: function (element, image) { |
|
122
|
|
|
var backgroundImage; |
|
123
|
|
|
if (image) { |
|
124
|
|
|
backgroundImage = 'url("' + image + '")'; |
|
125
|
|
|
} else { |
|
126
|
|
|
backgroundImage = 'none'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
element.css({ |
|
130
|
|
|
backgroundImage: backgroundImage, |
|
131
|
|
|
backgroundRepeat: 'no-repeat', |
|
132
|
|
|
backgroundPosition: 'right' |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
}; |
|
137
|
|
|
|
|
138
|
|
|
var VatNumberCheck = klass(VatNumberCheckObject); |
|
139
|
|
|
|